﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using UtilZ.Dotnet.WindowsDesktopEx.Base;

namespace UtilZ.Dotnet.WindowsDesktopEx.Winform
{
    /// <summary>
    /// Winfrom异步执行类
    /// </summary>
    /// <typeparam name="T">异步执行参数类型</typeparam>
    /// <typeparam name="TContainer">容器控件类型</typeparam>
    /// <typeparam name="TResult">异步执行返回值类型</typeparam>
    internal class WinformPartAsynExcute<T, TContainer, TResult> : WinformPartAsynExcuteAbs<T, TContainer, TResult> where TContainer : class
    {
        /// <summary>
        /// 遮罩层控件
        /// </summary>
        private Control _shadeControl = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        public WinformPartAsynExcute()
            : base()
        {

        }

        /// <summary>
        /// 创建默认遮罩层控件创建工厂对象
        /// </summary>
        /// <returns></returns>
        protected override IShadeControlFactory CreateDefaultShadeControlFactory()
        {
            return new WinformShadeControlFactoryV32();
        }

        /// <summary>
        /// 准备执行
        /// </summary>
        /// <param name="asynWaitPara"></param>
        /// <param name="containerControl"></param>
        protected override void ExcutePre(PartAsynWaitPara<T, TResult> asynWaitPara, TContainer containerControl)
        {
            //设置遮罩层控件
            asynWaitPara.AsynWait.ShadeBackground = PartAsynExcuteFactoryWinform.ConvertShadeBackground(asynWaitPara.AsynWaitBackground);
            var container = containerControl as Control;

            //添加遮罩层控件
            this._shadeControl = new UtilZ.Dotnet.WindowsDesktopEx.Winform.OpacityPanel() { Opacity = asynWaitPara.Opacity };
            this._shadeControl.Dock = DockStyle.Fill;
            container.Controls.Add(this._shadeControl);
            container.Controls.SetChildIndex(this._shadeControl, 0);

            var asynWaitControl = (Control)asynWaitPara.AsynWait;
            asynWaitControl.Anchor = AnchorStyles.None;
            asynWaitControl.Location = new System.Drawing.Point((container.Width - asynWaitControl.Width) / 2, (container.Height - asynWaitControl.Height) / 2);
            container.Controls.Add(asynWaitControl);
            container.Controls.SetChildIndex(asynWaitControl, 0);

            //禁用容器控件内的子控件的Tab焦点选中功能
            WinformPartAsynExcuteHelper.DisableTab(container, this._asynModifyControls);
        }

        /// <summary>
        /// 执行结束
        /// </summary>
        protected override void ExcuteFinished()
        {
            Control containerControl = base._containerControl as Control;
            WinformPartAsynExcuteHelper.EnableTab(this._asynModifyControls);
            containerControl.Controls.Remove(this._shadeControl);
            containerControl.Controls.Remove((Control)base._asynWaitPara.AsynWait);
            this._shadeControl.Dispose();
            this._shadeControl = null;
        }
    }
}
